package org.lionsoul.ip2region.txt;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;

import java.util.*;

/**
 * 加载ip.merge.txt 并仅取前3列：起始IP，终止IP，地区名称
 * 替换地区名称，成为地区编号
 * 从国->省->市->区进行按照cvs编号
 * <p>
 * 但是芋艿已经处理好area.csv
 * id,名称，类型（1-国家，2-省，3-市，4-区/县），父id
 * </p>
 */
public class YunaiIPMakerTest {

    // project path
    static String projectPath = System.getenv("project.path");


    /**
     * key1 level
     * key2 name
     * value code
     */
    final static Area AREA;
    final static List<IPMergeArea> ipAreas = new ArrayList<>(70_0000);

    static {
        if (null == projectPath) {
            projectPath = "/Users/yunai/Java/ip2region/";
        }
        String raw = FileUtil.readUtf8String(projectPath + "/data/area/area.csv");
        AREA = Area.buildAreaTree(raw);
        System.out.println("finish load area.csv " + (AREA != null));
        String ipMergeTxtRaw = FileUtil.readUtf8String(projectPath + "/data/ip.merge.txt");
        for (String record : ipMergeTxtRaw.split("\\n")) {
            ipAreas.add(new IPMergeArea(record));
        }
    }

    public static void main(String[] args) {

        testCountry();
        Set<String> missProvince = testProvince();
        testCity(missProvince);
        testArea();
    }

    public static Set<String> testCountry() {
        Set<String> hit = new HashSet<>(512);
        Set<String> miss = new HashSet<>(512);
        ipAreas.forEach(area -> {
            if (area.country != null
                && !hit.contains(area.country)) {
                Area tmp = AREA.findCountry(area.country);
                if (null == tmp) {
                    miss.add(area.country);
                } else {
                    hit.add(area.country);
                }
            }
        });
        if (miss.isEmpty()) {
            System.out.println("country test is ok.");
        } else {
            System.err.println("country test is fail " + miss.size() + "times： " + JSONUtil.toJsonStr(miss));
        }
        return miss;
    }

    public static Set<String> testProvince() {
        Set<String> hit = new HashSet<>(512);
        Set<String> miss = new HashSet<>(512);
        ipAreas.forEach(area -> {
            // 非中国跳过,不计数
            if (!"中国".equals(area.country)) {
                return;
            }
            if (area.province != null
                && !hit.contains(area.province)) {
                Area tmp = AREA.findProvince(area.province);
                if (null == tmp) {
                    miss.add(area.province);
                } else {
                    hit.add(area.province);
                }
            }
        });
        if (miss.isEmpty()) {
            System.out.println("province test is ok.");
        } else {
            System.err.println("province test is fail " + miss.size() + "times： " + JSONUtil.toJsonStr(miss));
        }
        return miss;
    }

    public static Set<IPMergeArea> testCity(Set<String> missProvince) {
        Set<Area> hit = new HashSet<>(512);
        Set<IPMergeArea> miss = new HashSet<>(512);
        ipAreas.forEach(area -> {
            // 非中国跳过,不计数
            if (!"中国".equals(area.country)) {
                return;
            }
            // 已经知道不会命中的省跳过，不计数
            if (area.province!= null && missProvince.contains(area.province)) {
                return ;
            }
            if (area.city != null) {
                Area tmp = AREA.findCity(area.province, area.city);
                if (null == tmp) {
                    miss.add(area);
                } else {
                    hit.add(tmp);
                }
            }
        });
        if (miss.isEmpty()) {
            System.out.println("city test is ok.");
        } else {
//            miss.sort(Comparator.comparing(o -> o.city));
            System.err.println("city test is fail " + miss.size() + "times");
            miss.forEach(m -> {
                System.err.println(m.toString());
            });
        }
        return miss;
    }

    public static void testArea() {
        // may ignore
    }


}
